package postgres

import (
	"context"
	"manager/model"
	"manager/model/entity"
)

// ListByIds 列表查询
func (a *alarmType) ListByIds(ctx context.Context, in *model.AlarmTypeExtraListRequest) (int, []*entity.AlarmType, error) {
	var (
		q          = GetDB(ctx).Model(&entity.AlarmType{})
		err        error
		total      int64
		alarmTypes []*entity.AlarmType
	)

	if in.Name != nil {
		q = q.Where("name = ?", in.Name)
	}

	if in.Content != nil {
		q = q.Where("content = ?", in.Content)
	}

	if in.Low != nil {
		q = q.Where("low = ?", in.Low)
	}

	if in.High != nil {
		q = q.Where("high = ?", in.High)
	}

	if in.UpdatedAt != nil {
		q = q.Where("updated_at = ?", in.UpdatedAt)
	}

	if len(in.Ids) > 0 {
		q = q.Where("id in ?", in.Ids)
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&alarmTypes).Error; err != nil {
		return 0, nil, err
	}
	return int(total), alarmTypes, nil
}
